package com.example.alibabanacosdiscoveryclient.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.alibabanacosdiscoveryclient.service.ConsumerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpHeaders;
import java.util.Arrays;
import java.util.Enumeration;

import static org.springframework.http.HttpStatus.BAD_GATEWAY;

/**
 * @createBy guoqiang
 * @createTime 2019/4/3
 * @describe
 */
@RestController
@Slf4j
@RequestMapping("/client")
@Tag(name="服务消费")
public class ConsumerController {
    @Autowired
    ConsumerService consumerService;

    @Autowired
    HttpServletRequest httpServletRequest;
    private int count=0;

    @SentinelResource(value = "black",fallback = "myFallback")
    @GetMapping("/black")
    @Operation(summary ="服务消费-黑名单")
    public String black(String origin) {
        return "我是消费端的黑名单方法，接口是通的,来源:"+origin;
    }

    @SentinelResource(value = "white")
    @GetMapping("/white")
    @Operation(summary ="服务消费-白名单")
    public String white(String origin) {
        return "我是消费端白名单的方法，接口是通的,来源:"+origin;
    }

    /*如果同时设置了 blockHandler限流的异常和fallback降级的处理，只会走blockHandler指定的方法*/
    @SentinelResource(value = "hello",/*blockHandler = "myBlockExceptionHandler",*/fallback = "myFallback")
    @GetMapping("/hello")
    @Operation(summary ="服务消费-测试调取服务端的方法")
    public String hello(String name) {
        log.info("我是消费端的接口，远程调用服务端接口是通的，参数是:" + name);
        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        return consumerService.hello(name);
    }
    @GetMapping("/test")
    @Operation(summary ="服务消费-test")
    public String test(String name) throws Exception {
        Thread.sleep(30000);
         return "name is :"+name;
//        return "测试gateway配置的熔断器，我是消费端的接口，远程调用服务端接口是通的，参数是:" + name;
    }
    @GetMapping("/retry")
    @Operation(summary ="服务消费-retry")
    public ResponseEntity<String> retry() throws Exception {
        count++;
        System.out.println("重试机制:"+count);
        if (count <= 3) {
               // 模拟 500 错误
               return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failure");
           } else {
               // 第四次请求成功
               return ResponseEntity.ok("Success after retries");
           }
    }
    @GetMapping("/rate")
    @Operation(summary ="服务消费-rateLimter")
    public String rate() throws Exception {
            return "rateLimter test";
    }
   public String myBlockExceptionHandler(String name, BlockException blockException){
        blockException.printStackTrace();
        return "我是限流的异常处理";
   }
    public String myFallback(String name){
        return "我是降级的异常处理";
    }
}
